package com.whiteape.book.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.whiteape.book.R;
import com.whiteape.book.common.RxEvent;
import com.whiteape.book.contract.MediaCollectionContract;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.model.type.RVLayoutType;
import com.whiteape.book.presenter.MediaCollectionPresenter;
import com.whiteape.book.ui.activity.BookReadActivity;
import com.whiteape.book.ui.activity.ComicReadActivity;
import com.whiteape.book.ui.adapter.MediaCollectionChildAdapter;
import com.whiteape.book.ui.base.BaseRVMVPFragment;
import com.whiteape.book.utils.RxUtils;
import com.whiteape.book.widget.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MediaCollectionChildFragment extends BaseRVMVPFragment<MediaCollectionContract.Presenter, CollectionMediaBean>
        implements MediaCollectionContract.View {

    private MediaCollectionChildAdapter listAdapter;
    private MediaCollectionChildAdapter gridAdapter;
    private List<CollectionMediaBean> data = new ArrayList<>();
    private static final String MEDIA_TYPE = "media_type";
    private int mediaType = MediaType.MEDIA_TYPE_NOVEL;

    @BindView(R.id.refresh_layout_loading)
    RefreshLayout refreshLayout;

    public static MediaCollectionChildFragment newInstance(int mediaType) {
        Bundle args = new Bundle();
        args.putInt(MEDIA_TYPE, mediaType);
        MediaCollectionChildFragment fragment = new MediaCollectionChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public MediaCollectionChildFragment() {
    }

    @Override
    protected boolean setBatchDeleteEnabled() {
        return true;
    }

    @Override
    protected List<BaseQuickAdapter<CollectionMediaBean, BaseViewHolder>> setAdapter() {
        listAdapter = new MediaCollectionChildAdapter(R.layout.item_collection_media_list, data);
        gridAdapter = new MediaCollectionChildAdapter(R.layout.item_collection_media_grid, data);

        listAdapter.setLayoutType(RVLayoutType.LIST);
        gridAdapter.setLayoutType(RVLayoutType.GRID);

        listAdapter.setSelectedList(mSelectedList);
        gridAdapter.setSelectedList(mSelectedList);

        List<BaseQuickAdapter<CollectionMediaBean, BaseViewHolder>> adapterList = new ArrayList<>();
        adapterList.add(listAdapter);
        adapterList.add(gridAdapter);
        return adapterList;
    }

    @Override
    protected boolean initListOrGrid() {
        return true;
    }

    @Override
    protected void deleteItems(List<CollectionMediaBean> itemList) {
        addDisposable(MediaStorageManager.getInstance().deleteCollMediaListInRx(itemList)
                .compose(RxUtils::toSimpleSingle).subscribe(v -> {
                    data.removeAll(itemList);
                    listAdapter.notifyDataSetChanged();
                    gridAdapter.notifyDataSetChanged();
                    refresh();
                }));
    }

    @Override
    protected boolean setAdapterModeSwitchEnabled() {
        return true;
    }

    @Override
    protected void onItemChildClickListener(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    protected void onItemClickListener(BaseQuickAdapter adapter, View view, int position) {

        if (getContext() == null) {
            return;
        }
        if (mediaType == MediaType.MEDIA_TYPE_NOVEL) {
            BookReadActivity.startActivity(getContext(), data.get(position), null, true, -1);
        } else if (mediaType == MediaType.MEDIA_TYPE_COMIC) {
            ComicReadActivity.startActivity(getContext(), data.get(position), null, true, null);
        }
    }

    @Override
    protected void onSelectStateChanged(boolean selectStateEnable) {
        listAdapter.setSelectedStateEnable(selectStateEnable);
        gridAdapter.setSelectedStateEnable(selectStateEnable);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (getArguments() != null) {
            mediaType = getArguments().getInt(MEDIA_TYPE, MediaType.MEDIA_TYPE_NOVEL);
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
//        String type = MediaType.getName(mediaType);
//        refreshLayout.setEmptyText("还没有" + type + "哦,点击右上角搜索下吧~");
        refreshLayout.showLoading();
        setEmptyView(R.layout.layout_empty_media);

    }
    //    @Override
//    protected void initView(Bundle savedInstanceState) {
//        super.initView(savedInstanceState);
//        rvContent.setLayoutManager(new LinearLayoutManager(getContext()));
//        if (getContext() != null) {
//            rvContent.addItemDecoration(new DividerItemDecoration(getContext()));
//        }
//        listAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
    //加载动画不仅仅是第一次
//        listAdapter.isFirstOnly(false);
    //下拉刷新
//        listAdapter.setUpFetchEnable(true);
//        rvContent.setAdapter(listAdapter);

//    }

//    @Override
//    protected void initEvent() {
//        super.initEvent();
//        listAdapter.setOnItemClickListener((adapter, view, position) -> {
//
//        });
//    }


    @Override
    public void finishRefresh(List<CollectionMediaBean> collectionMediaBeans) {
        data.clear();
        data.addAll(collectionMediaBeans);
        listAdapter.notifyDataSetChanged();
        gridAdapter.notifyDataSetChanged();
        refresh();
        refreshLayout.showFinish();

    }

    @Override
    public void finishUpdate() {

    }

    @Override
    public void showErrorTip(String error) {

    }

    @Override
    protected MediaCollectionPresenter bindPresenter() {
        return new MediaCollectionPresenter();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

//    @Override
//    protected int getContentId() {
//        return R.layout.fragment_collection_child;
//    }

    @Override
    protected void startLogic() {
        super.startLogic();

        mRecyclerView.setVisibility(View.INVISIBLE);
        mPresenter.refreshCollectionMedia(mediaType);
    }


    @Subscribe(thread = EventThread.MAIN_THREAD,
            tags = {@Tag(RxEvent.MEDIA_COLLECTION_ADD),
                    @Tag(RxEvent.MEDIA_COLLECTION_REMOVE),
                    @Tag(RxEvent.MEDIA_COLLECTION_REFRESH)})
    public void onCollectionStateChanged(CollectionMediaBean collectionMediaBean) {
        mPresenter.refreshCollectionMedia(mediaType);
    }
}
